package demoMod.nanaki.patches.events.exordium;

import basemod.ReflectionHacks;
import com.evacipated.cardcrawl.modthespire.lib.SpirePatch;
import com.evacipated.cardcrawl.modthespire.lib.SpireReturn;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.events.AbstractEvent;
import com.megacrit.cardcrawl.events.exordium.DeadAdventurer;
import com.megacrit.cardcrawl.helpers.EffectHelper;
import com.megacrit.cardcrawl.localization.EventStrings;
import demoMod.nanaki.NanakiMod;
import demoMod.nanaki.characters.NanakiCharacter;

public class DeadAdventurerPatch {
    public static final String ID = NanakiMod.makeID("Dead Adventurer");
    private static final EventStrings eventStrings;
    public static final String NAME;
    public static final String[] DESCRIPTIONS;
    public static final String[] OPTIONS;

    @SpirePatch(
            clz = DeadAdventurer.class,
            method = SpirePatch.CONSTRUCTOR
    )
    public static class PatchConstructor {
        public static void Postfix(DeadAdventurer event) {
            if (AbstractDungeon.player instanceof NanakiCharacter) {
                event.roomEventText.addDialogOption(OPTIONS[5]);
            }
        }
    }

    @SpirePatch(
            clz = DeadAdventurer.class,
            method = "buttonEffect"
    )
    public static class PatchButtonEffect {
        public static SpireReturn<Void> Prefix(DeadAdventurer event, int buttonPressed) {
            if (AbstractDungeon.player instanceof NanakiCharacter) {
                try {
                    Enum screen = ReflectionHacks.getPrivate(event, DeadAdventurer.class, "screen");
                    switch (screen.name()) {
                        case "INTRO":
                            switch (buttonPressed) {
                                case 2:
                                    if (AbstractDungeon.miscRng.random(0, 99) < 50) {
                                        event.roomEventText.updateBodyText(DESCRIPTIONS[11]);
                                        EffectHelper.gainGold(AbstractDungeon.player, 800.0F * Settings.xScale, AbstractDungeon.floorY, 50);
                                        AbstractDungeon.player.gainGold(50);
                                        AbstractEvent.logMetricGainGold("Dead Adventurer", "Searched '" + 1 + "' times", 50);
                                    } else {
                                        event.roomEventText.updateBodyText(DESCRIPTIONS[12]);
                                        AbstractEvent.logMetricGainGold("Dead Adventurer", "Searched '" + 0 + "' times", 0);
                                    }
                                    event.roomEventText.clearRemainingOptions();
                                    event.roomEventText.updateDialogOption(0, OPTIONS[1]);
                                    ReflectionHacks.setPrivate(event, DeadAdventurer.class, "screen", Enum.valueOf((Class<Enum>) Class.forName("com.megacrit.cardcrawl.events.exordium.DeadAdventurer$CUR_SCREEN"), "SUCCESS"));
                                    return SpireReturn.Return(null);
                                default:
                                    int numRewards = ReflectionHacks.getPrivate(event, DeadAdventurer.class, "numRewards");
                                    if (numRewards == 0) {
                                        event.roomEventText.removeDialogOption(2);
                                    }
                                    return SpireReturn.Continue();
                            }
                    }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
            return SpireReturn.Continue();
        }
    }

    static {
        eventStrings = CardCrawlGame.languagePack.getEventString(ID);
        NAME = eventStrings.NAME;
        DESCRIPTIONS = eventStrings.DESCRIPTIONS;
        OPTIONS = eventStrings.OPTIONS;
    }
}
